<?php // $this->load->library('enc'); ?>
<div class="box">
  <!-- box / title -->
  <div class="title">
    <h5>Data Produk</h5>
  </div>

  <div>
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
      <table class="item-list">
        <thead>
          <tr>
            <th colspan="3">Masukkan Kata Kunci (ICCID/MDN)</th>
          </tr>
        </thead>
        <tr>
          <td width="100px">MDN atau ICCID </td>
          <td><input type="text" name="key" id="key" style="width: 320px" /></td>
          <td><button >Cari</button></td>
        </tr>
      </table>
    </form>
  </div>
  
  <?php if ($result) : ?>
  <div id="search-result">
    <table class="item-list">
      <thead>
        <tr>
          <th colspan="2">Informasi Item</th>
        </tr>
      </thead>
      <tr>
        <td width="">ICCID</td>
        <td class="iccid"><?php echo $result["item"]["iccid"] ?></td>
      </tr>
      <tr>
        <td>MDN</td>
        <td class="mdn"><?php echo $result["item"]["mdn"] ?></td>
      </tr>
      <tr>
        <td>Nama Item</td>
        <td class="item_name"><?php echo $result["item"]["item_name"] ?></td>
      </tr>
      <tr>
        <td>Item Group</td>
        <td class="item_name"><?php echo $result["item"]["item_group_name"] ?></td>
      </tr>
      <tr>
        <td>Harga Retail</td>
        <td class="item_name">Rp. <?php echo number_format($result["item"]["price"], 2, ',', '.') ?></td>
      </tr>
      <tr>
        <td>Nama Distributor</td>
        <td class="distibutor_name"><?php echo $result["item"]["distributor_name"] ?></td>
      </tr>
      <tr>
        <td>Regional</td>
        <td class="regional"><?php echo $result["item"]["region"] ?></td>
      </tr>
      <tr>
        <td>Cluster</td>
        <td class="cluster"><?php echo $result["item"]["cluster"] ?></td>
      </tr>
      <tr>
        <td>MDN Aktif</td>
        <td class="status">
        <?php 
          if($result["item"]["dwh_activated"] == 1) 
            echo 'Aktif';
          else
            echo 'Belum Aktif';
        ?>
        </td>
      </tr>
      <tr>
        <td>Status Transaksi Terakhir</td>
        <td class="status">
        <?php 
          if($result["item"])
          {
            if(isset($result["item"]["istatus"]) == '15')
              { echo $result["item"]["istatus"].': Penjualan TMS'; }
            else
              { echo $result["item"]["istatus"].': Masih dibawa oleh sales'; }
          }
          else
            { echo $result["item"]["istatus"].': Masih Ada di Cluster'; }
        ?>
        </td>
      </tr>
      
    </table>
  </div>

  <?php if (count($result["scan"]) > 0 ) : ?>
  <div id="scan-history">
    <table class="item-list">
      <thead>
        <tr>
          <th colspan = "7" >SCAN HISTORY</th>
        </tr>
        <tr>
          <th>No</th>
          <th>Scan ID</th>
          <th>Time Stamp</th>
          <th>User ID</th>
          <th>User Name</th>
          <th>Status</th>
          <th>Device</th>
        </tr>
      </thead>
      <tbody>
        <?php $x=1; foreach ($result["scan"] as $key) : ?>
          <tr>
          <td align="center" ><?php echo $x++; ?></th>
          <td align="center" >
            <a href="<?php echo base_url().'index.php/scan_history/getDetail/'.$this->enc->fogit($key["scan_in_id"]); ?>" >
            <?php echo $key["scan_in_id"];?></a>
          </td>
          <td><?php echo $key["created_on"] ;?></td>
          <td><?php echo $key["user_id"] ;?></td>
          <td><?php echo $key["user_name"] ;?></td>
          <td>
            <?php 
              $iccid = explode('#', $key["iccid"]);
              // print_r($iccid);
              if($key["istatus"] == 1)
                { $status = 'Masih dibawa oleh sales'; }
              elseif (isset($iccid[1])) {
                if($iccid[1] == 'rcp') 
                  { $status = 'Telah kembali ke cluster dengan nomor transaksi receipt <a href="'.base_url().'index.php/receipt/getDetail/'.$this->enc->fogit($iccid[2]).'/'.$this->enc->fogit($key["user_id"]).'">'. $iccid[2].'</a>'; }
                else
                  { $status = 'Telah terjual dan dibuat SO dengan nomor transaksi SO <a href="'.base_url().'index.php/sales_order/getDetail/'.$this->enc->fogit($iccid[2]).'/'.$this->enc->fogit($key["user_id"]).'">'. $iccid[2].'</a>'; }
              }
              else
                { $status = 'Sudah terjual dan belum bikin SO atau konsinyasi'; }

              echo $status ;
            ?>
          </td>
          <td><?php echo $key["acess_via"] ;?></td>
          </tr>
        <?php endforeach ;?>
      </tbody>
    </table>
  </div>

  <?php endif;  if (count($result["receipt"]) > 0 ) : ?>
  <div id="receipt-history">
    <table class="item-list">
      <thead>
        <tr>
          <th colspan = "6" >RECEIPT HISTORY</th>
        </tr>
        <tr>
          <th>No</th>
          <th>Receipt ID</th>
          <th>Tanggal Receipt</th>
          <th>Receipt By</th>
          <th>Sales Name</th>
        </tr>
      </thead>
      <tbody>
        <?php $x=1; foreach ($result["receipt"] as $key) : ?>
          <tr>
            <td align="center" ><?php echo $x++; ?></th>
            <td align="center" >
              <a href="<?php echo base_url().'index.php/receipt/getDetail/'.$this->enc->fogit($key["receipt_id"]); ?>" >
              <?php echo $key["receipt_id"];?></a>
            </td>
            <td><?php echo $key["created_on"] ;?></td>
            <td><?php echo $key["user_id"].' '.$key["created_by"] ;?></td>
            <td><?php echo $key["sales_id"].' '.$key["user_name"] ;?></td>
          </tr>
        <?php endforeach ;?>
      </tbody>
    </table>
  </div>

  <?php endif; if (count($result["sold"])) : ?>
  <div id="sell-in">
    <table class="item-list">
      <thead>
        <tr>
          <th colspan ="7" >SELL HISTORY</th>
        </tr>
        <tr>
          <th>Sell In ID</th>
          <th>Time Stamp</th>
          <th>Channel ID</th>
          <th>Channel Name</th>
          <th>Sales ID</th>
          <th>Sales Name</th>
          <th>Device</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td align="center">
            <a href="<?php echo base_url().'index.php/sell/getDetail/'.$this->enc->fogit($result["sold"]["sell_in_id"]); ?>" >
            <?php echo $result["sold"]["sell_in_id"];?>
            </a>
          </td>
          <td><?php echo $result["sold"]["created_on"];?></td>
          <td><?php echo $result["sold"]["channel_id"];?></td>
          <td><?php echo $result["sold"]["channel_name"];?></td>
          <td><?php echo $result["sold"]["user_id"];?></td>
          <td><?php echo $result["sold"]["user_name"];?></td>
          <td><?php echo $result["sold"]["acess_via"];?></td>
        </tr>
      </tbody>
    </table>    
  </div>

  <?php endif; if (count($result["consignment"])) : ?>
  <div id="sell-in">
    <table class="item-list">
      <thead>
        <tr>
          <th colspan ="8" >KONSINYASI HISTORY</th>
        </tr>
        <tr>
          <th>ID Konsinyasi</th>
          <th>Tanggal</th>
          <th>Channel ID</th>
          <th>Channel Name</th>
          <th>Sales ID</th>
          <th>Sales Name</th>
          <th>Status</th>
          <th>Device</th>
        </tr>
      </thead>
      <tbody>
        <?php $x=1; foreach ($result["consignment"] as $key) : ?>
        <tr>
          <td align="center">
            <a href="<?php echo base_url().'index.php/consignment_takeout/index/'.$this->enc->fogit($key["consignment_id"]); ?>" >
            <?php echo $key["consignment_id"];?>
            </a>
          </td>
          <td><?php echo $key["created_on"];?></td>
          <td><?php echo $key["channel_id"];?></td>
          <td><?php echo $key["channel_name"];?></td>
          <td><?php echo $key["user_id"];?></td>
          <td><?php echo $key["user_name"];?></td>
          <td>
            <?php 
              if($key["istatus"] == 1)
                { echo 'Konsinyasi baru'; }
              elseif($key["istatus"] == 2)
                { echo 'Konsinyasi tidak terjual'; }
              else
                { echo 'Konsinyasi terjual'; }
            ?>
          </td>
          <td><?php echo $key["acess_via"];?></td>
        </tr>
        <?php endforeach ;?>
      </tbody>
    </table>    
  </div>

  <?php 
    endif; else : if($search == 1) :
  ?>
  <div id="search-result">
    <table class="item-list">
      <tr>
        <td>Item dengan ICCID yang dimaksud tidak terdaftar</td>
      </tr>
    </table>
  </div>
  <?php 
    endif; 
    endif; // end if search result
  ?>

</div>